/**
 *
 */
package com.suieqian.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.suieqian.dao.UserDao;
import com.suieqian.entity.User;
import com.suieqian.enums.DelStatusEnum;
import com.suieqian.exception.ServiceLogicException;
import com.suieqian.service.ISecurityService;
import com.suieqian.util.MD5Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;


/**
 *
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年2月9日 下午9:53:07 
 *
 */
@Service("securityService")
@Transactional
public class SecurityServiceImpl implements ISecurityService {

    @Resource
    private UserDao userDao;

    @Value("${operate.aeskey}")
    private String aeskey;

    @Override
    public User login(String account, String pwd) {

        if (StrUtil.isBlank(account)) {
            throw new ServiceLogicException("登录账户不能为空");
        }
        if (StrUtil.isBlank(pwd)) {
            throw new ServiceLogicException("登录密码不能为空");
        }

        User dbUser = userDao.selectByAccount(account);
        if (Objects.isNull(dbUser)) {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery();
            userLambdaQueryWrapper.eq(User::getPhone, account);
            userLambdaQueryWrapper.eq(User::getDelStatus, DelStatusEnum.NORMAL.getCode());
            dbUser = userDao.selectOne(userLambdaQueryWrapper);
        }

        if (Objects.isNull(dbUser)) {
            throw new ServiceLogicException("该用户账号不存在");
        }

        if (dbUser.getDelStatus() == 1) {
            throw new ServiceLogicException("该用户账号已失效");
        }

        String md5Pwd = null;
        try {
            md5Pwd = MD5Util.md5Encode(pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!md5Pwd.equals(dbUser.getUserPwd())) {
            throw new ServiceLogicException("账号或密码不正确");
        }
        User loginUpdate = new User();
        loginUpdate.setUserId(dbUser.getUserId());
        loginUpdate.setLoginTimes(dbUser.getLoginTimes() == null ? 1 : dbUser.getLoginTimes() + 1);
        userDao.updateById(loginUpdate);

        return dbUser;
    }

    @Override
    public User register(User user) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isExistUser(String account) {

        if (StrUtil.isBlank(account)) {
            return false;
        }

        User user = userDao.selectByAccount(account);
        if (Objects.isNull(user)) {
            return false;
        }

        return true;
    }

    @Override
    public User checkCookie(String cookieValue) {
        // 调试时候使用
        if ("cookie".equals(cookieValue)) {
            return userDao.selectByAccount("admin");
        }
        AES des = SecureUtil.aes(aeskey.getBytes());
        try {
            String s = des.decryptStr(Base64.decode(cookieValue));
            if (s.indexOf("#user#") == -1) {
                return null;
            }
            String[] split = s.split("#user#");
            User user = userDao.selectByAccount(split[0]);

            return user;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
